{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SELECT within SELECT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " ·········\n"
     ]
    }
   ],
   "source": [
    "# Prerequesites\n",
    "import getpass\n",
    "%load_ext sql\n",
    "pwd = getpass.getpass()\n",
    "# %sql mysql+pymysql://root:$pwd@localhost:3306/sqlzoo\n",
    "%sql postgresql://postgres:$pwd@localhost/sqlzoo\n",
    "%config SqlMagic.displaylimit = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Bigger than Russia\n",
    "\n",
    "**List each country name where the population is larger than that of 'Russia'.**\n",
    "\n",
    "```\n",
    "world(name, continent, area, population, gdp)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "8 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Bangladesh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Brazil</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>China</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>India</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Indonesia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Nigeria</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Pakistan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>United States</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Bangladesh',),\n",
       " ('Brazil',),\n",
       " ('China',),\n",
       " ('India',),\n",
       " ('Indonesia',),\n",
       " ('Nigeria',),\n",
       " ('Pakistan',),\n",
       " ('United States',)]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT name FROM world\n",
    "  WHERE population > (SELECT population FROM world WHERE name='Russia');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Richer than UK\n",
    "\n",
    "**Show the countries in Europe with a per capita GDP greater than 'United Kingdom'.**\n",
    "\n",
    "> _Per Capita GDP_   \n",
    "> The per capita GDP is the gdp/population"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "17 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Andorra</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Austria</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Belgium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Denmark</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Finland</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>France</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Germany</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Iceland</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ireland</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Liechtenstein</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Luxembourg</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Monaco</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Netherlands</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Norway</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>San Marino</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Sweden</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Switzerland</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Andorra',),\n",
       " ('Austria',),\n",
       " ('Belgium',),\n",
       " ('Denmark',),\n",
       " ('Finland',),\n",
       " ('France',),\n",
       " ('Germany',),\n",
       " ('Iceland',),\n",
       " ('Ireland',),\n",
       " ('Liechtenstein',),\n",
       " ('Luxembourg',),\n",
       " ('Monaco',),\n",
       " ('Netherlands',),\n",
       " ('Norway',),\n",
       " ('San Marino',),\n",
       " ('Sweden',),\n",
       " ('Switzerland',)]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT name FROM world\n",
    "WHERE gdp/population > \n",
    " (SELECT gdp/population FROM world WHERE name = 'United Kingdom') \n",
    "    AND continent = 'Europe';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Neighbours of Argentina and Australia\n",
    "\n",
    "\n",
    "List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "27 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "        <th>continent</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Argentina</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Australia</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Bolivia</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Brazil</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Chile</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Colombia</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ecuador</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Fiji</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Guyana</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Kiribati</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Marshall Islands</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Micronesia, Federated States of</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Nauru</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>New Zealand</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Palau</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Papua New Guinea</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Paraguay</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Peru</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Saint Vincent and the Grenadines</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Samoa</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">27 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[('Argentina', 'South America'),\n",
       " ('Australia', 'Oceania'),\n",
       " ('Bolivia', 'South America'),\n",
       " ('Brazil', 'South America'),\n",
       " ('Chile', 'South America'),\n",
       " ('Colombia', 'South America'),\n",
       " ('Ecuador', 'South America'),\n",
       " ('Fiji', 'Oceania'),\n",
       " ('Guyana', 'South America'),\n",
       " ('Kiribati', 'Oceania'),\n",
       " ('Marshall Islands', 'Oceania'),\n",
       " ('Micronesia, Federated States of', 'Oceania'),\n",
       " ('Nauru', 'Oceania'),\n",
       " ('New Zealand', 'Oceania'),\n",
       " ('Palau', 'Oceania'),\n",
       " ('Papua New Guinea', 'Oceania'),\n",
       " ('Paraguay', 'South America'),\n",
       " ('Peru', 'South America'),\n",
       " ('Saint Vincent and the Grenadines', 'South America'),\n",
       " ('Samoa', 'Oceania'),\n",
       " ('Solomon Islands', 'Oceania'),\n",
       " ('Suriname', 'South America'),\n",
       " ('Tonga', 'Oceania'),\n",
       " ('Tuvalu', 'Oceania'),\n",
       " ('Uruguay', 'South America'),\n",
       " ('Vanuatu', 'Oceania'),\n",
       " ('Venezuela', 'South America')]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT name, continent FROM world\n",
    "WHERE continent IN \n",
    " (SELECT continent FROM world WHERE name IN ('Argentina', 'Australia'))\n",
    "ORDER BY name;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Between Canada and Poland\n",
    "\n",
    "Which country has a population that is more than Canada but less than Poland? Show the name and the population."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "2 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "        <th>population</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Iraq</td>\n",
       "        <td>36004552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Sudan</td>\n",
       "        <td>37289406</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Iraq', Decimal('36004552')), ('Sudan', Decimal('37289406'))]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT name, population FROM world\n",
    "WHERE population > \n",
    " (SELECT population FROM world WHERE name = 'Canada') \n",
    "    AND population <\n",
    "    (SELECT population FROM world WHERE name = 'Poland');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Percentages of Germany\n",
    "\n",
    "Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.\n",
    "\n",
    "**Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.**\n",
    "\n",
    "The format should be Name, Percentage for example:\n",
    "\n",
    "name\t| percentage\n",
    "--------|-----------\n",
    "Albania\t| 3%\n",
    "Andorra\t| 0%\n",
    "Austria\t| 11%\n",
    "...\t| ...\n",
    "\n",
    "> _Decimal places_   \n",
    "> You can use the function ROUND to remove the decimal places.\n",
    "\n",
    "> _Percent symbol %_   \n",
    "> You can use the function CONCAT to add the percentage symbol.\n",
    "\n",
    "[To get a well rounded view of the important features of SQL you should move on to the next tutorial concerning aggregates.](https://sqlzoo.net/wiki/SUM_and_COUNT)\n",
    "\n",
    "To gain an absurdly detailed view of one insignificant feature of the language, read on.\n",
    "\n",
    "We can use the word `ALL` to allow >= or > or < or <=to act over a list. For example, you can find the largest country in the world, by population with this query:\n",
    "\n",
    "```sql\n",
    "SELECT name\n",
    "  FROM world\n",
    " WHERE population >= ALL(SELECT population\n",
    "                           FROM world\n",
    "                          WHERE population>0)\n",
    "```\n",
    "\n",
    "You need the condition **population>0** in the sub-query as some countries have **null** for population."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "44 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "        <th>pct</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Albania</td>\n",
       "        <td>3%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Andorra</td>\n",
       "        <td>0%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Austria</td>\n",
       "        <td>11%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Belarus</td>\n",
       "        <td>12%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Belgium</td>\n",
       "        <td>14%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Bosnia and Herzegovina</td>\n",
       "        <td>5%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Bulgaria</td>\n",
       "        <td>9%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Croatia</td>\n",
       "        <td>5%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Czech Republic</td>\n",
       "        <td>13%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Denmark</td>\n",
       "        <td>7%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Estonia</td>\n",
       "        <td>2%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Finland</td>\n",
       "        <td>7%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>France</td>\n",
       "        <td>82%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Germany</td>\n",
       "        <td>100%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Greece</td>\n",
       "        <td>14%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Hungary</td>\n",
       "        <td>12%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Iceland</td>\n",
       "        <td>0%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ireland</td>\n",
       "        <td>6%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Italy</td>\n",
       "        <td>75%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Kazakhstan</td>\n",
       "        <td>21%</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">44 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[('Albania', '3%'),\n",
       " ('Andorra', '0%'),\n",
       " ('Austria', '11%'),\n",
       " ('Belarus', '12%'),\n",
       " ('Belgium', '14%'),\n",
       " ('Bosnia and Herzegovina', '5%'),\n",
       " ('Bulgaria', '9%'),\n",
       " ('Croatia', '5%'),\n",
       " ('Czech Republic', '13%'),\n",
       " ('Denmark', '7%'),\n",
       " ('Estonia', '2%'),\n",
       " ('Finland', '7%'),\n",
       " ('France', '82%'),\n",
       " ('Germany', '100%'),\n",
       " ('Greece', '14%'),\n",
       " ('Hungary', '12%'),\n",
       " ('Iceland', '0%'),\n",
       " ('Ireland', '6%'),\n",
       " ('Italy', '75%'),\n",
       " ('Kazakhstan', '21%'),\n",
       " ('Latvia', '2%'),\n",
       " ('Liechtenstein', '0%'),\n",
       " ('Lithuania', '4%'),\n",
       " ('Luxembourg', '1%'),\n",
       " ('Macedonia', '3%'),\n",
       " ('Malta', '1%'),\n",
       " ('Moldova', '4%'),\n",
       " ('Monaco', '0%'),\n",
       " ('Montenegro', '1%'),\n",
       " ('Netherlands', '21%'),\n",
       " ('Norway', '6%'),\n",
       " ('Poland', '48%'),\n",
       " ('Portugal', '13%'),\n",
       " ('Romania', '25%'),\n",
       " ('San Marino', '0%'),\n",
       " ('Serbia', '9%'),\n",
       " ('Slovakia', '7%'),\n",
       " ('Slovenia', '3%'),\n",
       " ('Spain', '58%'),\n",
       " ('Sweden', '12%'),\n",
       " ('Switzerland', '10%'),\n",
       " ('Ukraine', '53%'),\n",
       " ('United Kingdom', '79%'),\n",
       " ('Vatican City', '0%')]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT name, \n",
    " CONCAT(ROUND(100 * population/(SELECT population FROM world WHERE name = 'Germany'), 0), '%') AS pct\n",
    "    FROM world\n",
    "    WHERE continent = 'Europe';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Bigger than every country in Europe\n",
    "\n",
    "Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)\n",
    "\n",
    "We can refer to values in the outer SELECT within the inner SELECT. We can name the tables so that we can tell the difference between the inner and outer versions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "3 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>China</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Japan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>United States</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('China',), ('Japan',), ('United States',)]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT name FROM world\n",
    "WHERE gdp > ALL(SELECT gdp FROM world WHERE continent = 'Europe' AND gdp>0);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Largest in each continent\n",
    "\n",
    "**Find the largest country (by area) in each continent, show the continent, the name and the area:**\n",
    "\n",
    "```sql\n",
    "SELECT continent, name, population FROM world x\n",
    "  WHERE population >= ALL\n",
    "    (SELECT population FROM world y\n",
    "        WHERE y.continent=x.continent\n",
    "          AND population>0)\n",
    "```\n",
    "\n",
    "> __The above example is known as a correlated or synchronized sub-query.__   \n",
    "> \n",
    "> Using correlated subqueries\n",
    "> A correlated subquery works like a nested loop: the subquery only has access to rows related to a single > record at a time in the outer query. The technique relies on table aliases to identify two different uses of the same table, one in the outer query and the other in the subquery.\n",
    "> \n",
    "> One way to interpret the line in the **WHERE** clause that references the two table is _“… where the correlated values are the same”._\n",
    "> \n",
    "> In the example provided, you would say _“select the country details from world where the population is greater than or equal to the population of all countries where the continent is the same”._"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "8 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>continent</th>\n",
       "        <th>name</th>\n",
       "        <th>area</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Africa</td>\n",
       "        <td>Algeria</td>\n",
       "        <td>2381741</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Oceania</td>\n",
       "        <td>Australia</td>\n",
       "        <td>7692024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>South America</td>\n",
       "        <td>Brazil</td>\n",
       "        <td>8515767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>North America</td>\n",
       "        <td>Canada</td>\n",
       "        <td>9984670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Asia</td>\n",
       "        <td>China</td>\n",
       "        <td>9596961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Caribbean</td>\n",
       "        <td>Cuba</td>\n",
       "        <td>109884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Europe</td>\n",
       "        <td>Kazakhstan</td>\n",
       "        <td>2724900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Eurasia</td>\n",
       "        <td>Russia</td>\n",
       "        <td>17125242</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Africa', 'Algeria', Decimal('2381741')),\n",
       " ('Oceania', 'Australia', Decimal('7692024')),\n",
       " ('South America', 'Brazil', Decimal('8515767')),\n",
       " ('North America', 'Canada', Decimal('9984670')),\n",
       " ('Asia', 'China', Decimal('9596961')),\n",
       " ('Caribbean', 'Cuba', Decimal('109884')),\n",
       " ('Europe', 'Kazakhstan', Decimal('2724900')),\n",
       " ('Eurasia', 'Russia', Decimal('17125242'))]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT continent, name, area FROM world x\n",
    "  WHERE area >= ALL\n",
    "    (SELECT area FROM world y\n",
    "       WHERE y.continent=x.continent AND area>0);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. First country of each continent (alphabetically)\n",
    "\n",
    "**List each continent and the name of the country that comes first alphabetically.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "8 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>continent</th>\n",
       "        <th>name</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Asia</td>\n",
       "        <td>Afghanistan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Europe</td>\n",
       "        <td>Albania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Africa</td>\n",
       "        <td>Algeria</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Caribbean</td>\n",
       "        <td>Antigua and Barbuda</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>South America</td>\n",
       "        <td>Argentina</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Eurasia</td>\n",
       "        <td>Armenia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Oceania</td>\n",
       "        <td>Australia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>North America</td>\n",
       "        <td>Belize</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Asia', 'Afghanistan'),\n",
       " ('Europe', 'Albania'),\n",
       " ('Africa', 'Algeria'),\n",
       " ('Caribbean', 'Antigua and Barbuda'),\n",
       " ('South America', 'Argentina'),\n",
       " ('Eurasia', 'Armenia'),\n",
       " ('Oceania', 'Australia'),\n",
       " ('North America', 'Belize')]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT continent, name\n",
    "FROM world x\n",
    "WHERE name <= ALL\n",
    " (SELECT name FROM world y\n",
    "  WHERE y.continent = x.continent AND y.name IS NOT NULL\n",
    "  ORDER By name);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9. Difficult Questions That Utilize Techniques Not Covered In Prior Sections\n",
    "\n",
    "**Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show name, continent and population.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "25 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "        <th>continent</th>\n",
       "        <th>population</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Antigua and Barbuda</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>86295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Australia</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>23545500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Bahamas</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>351461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Barbados</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>285000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Cuba</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>11167325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Dominica</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>71293</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Dominican Republic</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>9445281</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Fiji</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>858038</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Grenada</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>103328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Haiti</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>10413211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Jamaica</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>2717991</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Kiribati</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>106461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Marshall Islands</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>56086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Micronesia, Federated States of</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>101351</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Nauru</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>9945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>New Zealand</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>4538520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Palau</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>20901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Papua New Guinea</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>7398500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Saint Lucia</td>\n",
       "        <td>Caribbean</td>\n",
       "        <td>180000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Samoa</td>\n",
       "        <td>Oceania</td>\n",
       "        <td>187820</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">25 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[('Antigua and Barbuda', 'Caribbean', Decimal('86295')),\n",
       " ('Australia', 'Oceania', Decimal('23545500')),\n",
       " ('Bahamas', 'Caribbean', Decimal('351461')),\n",
       " ('Barbados', 'Caribbean', Decimal('285000')),\n",
       " ('Cuba', 'Caribbean', Decimal('11167325')),\n",
       " ('Dominica', 'Caribbean', Decimal('71293')),\n",
       " ('Dominican Republic', 'Caribbean', Decimal('9445281')),\n",
       " ('Fiji', 'Oceania', Decimal('858038')),\n",
       " ('Grenada', 'Caribbean', Decimal('103328')),\n",
       " ('Haiti', 'Caribbean', Decimal('10413211')),\n",
       " ('Jamaica', 'Caribbean', Decimal('2717991')),\n",
       " ('Kiribati', 'Oceania', Decimal('106461')),\n",
       " ('Marshall Islands', 'Oceania', Decimal('56086')),\n",
       " ('Micronesia, Federated States of', 'Oceania', Decimal('101351')),\n",
       " ('Nauru', 'Oceania', Decimal('9945')),\n",
       " ('New Zealand', 'Oceania', Decimal('4538520')),\n",
       " ('Palau', 'Oceania', Decimal('20901')),\n",
       " ('Papua New Guinea', 'Oceania', Decimal('7398500')),\n",
       " ('Saint Lucia', 'Caribbean', Decimal('180000')),\n",
       " ('Samoa', 'Oceania', Decimal('187820')),\n",
       " ('Solomon Islands', 'Oceania', Decimal('581344')),\n",
       " ('Tonga', 'Oceania', Decimal('103036')),\n",
       " ('Trinidad and Tobago', 'Caribbean', Decimal('1328019')),\n",
       " ('Tuvalu', 'Oceania', Decimal('11323')),\n",
       " ('Vanuatu', 'Oceania', Decimal('264652'))]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT name, continent, population\n",
    "FROM world x\n",
    "WHERE continent NOT IN \n",
    "  (SELECT continent FROM world WHERE population > 25000000);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.\n",
    "\n",
    "**Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "3 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "        <th>continent</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Australia</td>\n",
       "        <td>Oceania</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Brazil</td>\n",
       "        <td>South America</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Russia</td>\n",
       "        <td>Eurasia</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Australia', 'Oceania'), ('Brazil', 'South America'), ('Russia', 'Eurasia')]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT name, continent\n",
    "FROM world x\n",
    "WHERE population > ALL\n",
    "  (SELECT population*3 FROM world y\n",
    "   WHERE y.continent = x.continent AND area > 0 AND y.name <> x.name);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
